package com.zhang.zhangapp.wxapi;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhang.zhangapp.MainActivity;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.Constant;
import com.zhang.zhangapp.utils.ActivityManager;
import com.zhang.zhangapp.utils.T;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by Administrator on 2018/4/27.
 */
@ContentView(R.layout.activity_pay_success)
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    public final static String PAY_SUCCESS = "paysuccess";
    public final static String PAY_FAIL = "pay_fail";
    private IWXAPI api;
    private LocalBroadcastManager localBroadcastManager;
    @ViewInject(R.id.action_bar_title)
    TextView titleText;
    @ViewInject(R.id.result_text)
    TextView resultText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_pay_success);
        x.view().inject(this);
        titleText.setText("支付结果");
        api = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID);
        api.handleIntent(getIntent(), this);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
//        ActivityManager.getActivityManager().addActivity(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Event(type = View.OnClickListener.class, value = R.id.action_bar_left_image)
    private void back(View view) {
        this.finish();
    }

    @Override
    public void onReq(BaseReq req) {
        if (req.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

        }
//        if (req.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            // 支付成功
//            if (req.errCode == 0) {
//                ((MainActivity) ActivityManager.getActivityManager().getActivty(MainActivity.class)).updateUserInfo();
//                Intent intent = new Intent(PAY_SUCCESS);
//                localBroadcastManager.sendBroadcast(intent);
//            } else if (req.errCode == -1) {
//            } else if (req.errCode == -2) {
//            }
//        }
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onResp(BaseResp resp) {

        //Log.d("TAG", "onPayFinish, errCode = " + resp.errCode);
        // Toast.makeText(this, "" + resp.errCode + "pay", Toast.LENGTH_SHORT).show();
        ((MainActivity) ActivityManager.getActivityManager().getActivty(MainActivity.class)).updateUserInfo();
        Intent intent = null;
        // 支付成功
        if (resp.errCode == 0) {
            resultText.setText("支付成功");
            intent = new Intent(PAY_SUCCESS);
        } else if (resp.errCode == -2) {
            resultText.setText("支付取消");
            intent = new Intent(PAY_FAIL);
        } else {
            resultText.setText("支付失败");
            intent = new Intent(PAY_FAIL);
        }
        localBroadcastManager.sendBroadcast(intent);
    }
}
